﻿Imports System.ComponentModel
Imports System.Text
Imports System.Xml
Imports System.IO

Namespace rtmpGUI
	Partial Public Class Options
		Inherits Form

		Private frm As New Main()
		Private vlcfile As String = String.Empty
		Private list As String = String.Empty
		Private updates As String = String.Empty
		Private altload As String = String.Empty
		Private suppress As String = String.Empty
		Private default_commands As String = String.Empty

		Private Shared _instance As Options
		Public Sub New()
			InitializeComponent()

			tabControl1.ImageList = imageList1
			tabPage1.ImageIndex = 0
			tabPage2.ImageIndex = 1

			Me.Font = SystemFonts.MessageBoxFont
		End Sub

		Public Shared Function Instance() As Options
			If _instance Is Nothing Then
				_instance = New Options()
			End If
			Return _instance

		End Function


		Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
			If findVLC.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
				txtVLCloc.Text = findVLC.FileName.ToString()
				txtVLCloc.Text = """" & txtVLCloc.Text & """"
			End If
		End Sub

		Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
			SaveSettings()
			Me.Hide()
		End Sub

		Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button3.Click
			Me.Hide()
		End Sub

		Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button4.Click
			If findChannels.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
				txtLocList.Text = findChannels.FileName.ToString()
			End If
		End Sub


		Public Sub SaveSettings()
			Using tw = New XmlTextWriter(Application.StartupPath.ToString() & "\config.xml", Nothing)
				tw.Formatting = Formatting.Indented
				tw.WriteStartDocument()
				tw.WriteStartElement("rtmpGUI", String.Empty)

				tw.WriteStartElement("vlc-loc", "")
				tw.WriteString(txtVLCloc.Text)
				tw.WriteEndElement()


				tw.WriteStartElement("list", "")
				If chkStartList.Checked = True Then
					tw.WriteAttributeString("remote", "true")
				Else
					tw.WriteAttributeString("remote", "false")
					tw.WriteString(txtLocList.Text)
				End If

				tw.WriteEndElement()

				tw.WriteStartElement("updates", "")
				If chkCheckUpdates.Checked = True Then
					tw.WriteString("true")
				Else
					tw.WriteString("false")
				End If
				tw.WriteEndElement()

				tw.WriteStartElement("altpage", "")
				If chkWebPage.Checked = True Then
					tw.WriteAttributeString("load", "true")
				Else
					tw.WriteAttributeString("load", "false")
				End If
				tw.WriteString(txtWeb.Text)
				tw.WriteEndElement()

				tw.WriteStartElement("suppress", "")
				If chkSuppress.Checked = True Then
					tw.WriteString("true")
				Else
					tw.WriteString("false")
				End If
				tw.WriteEndElement()

				tw.WriteStartElement("default_commands", "")
				If chkCommands.Checked = True Then
					tw.WriteString("true")
				Else
					tw.WriteString("false")
				End If
				tw.WriteEndElement()

				tw.WriteStartElement("api", "")
				tw.WriteAttributeString("user", txtAPIuser.Text)
				tw.WriteAttributeString("key", txtAPIkey.Text)
				tw.WriteEndElement()

				tw.WriteEndElement()
				tw.WriteEndDocument()
			End Using
		End Sub

		Private Sub LoadSettings()
			Dim xDoc As New XmlDocument()
			Try
				xDoc.Load(Application.StartupPath.ToString() & "\config.xml")
				txtVLCloc.Text = xDoc.GetElementsByTagName("vlc-loc")(0).InnerText

				txtAPIuser.Text = xDoc.SelectSingleNode("/rtmpGUI/api/@user").Value
				txtAPIkey.Text = xDoc.SelectSingleNode("/rtmpGUI/api/@key").Value

				altload = xDoc.SelectSingleNode("/rtmpGUI/altpage/@load").Value
				txtWeb.Text = xDoc.GetElementsByTagName("altpage")(0).InnerText

				list = xDoc.SelectSingleNode("/rtmpGUI/list/@remote").Value
				txtLocList.Text = xDoc.GetElementsByTagName("list")(0).InnerText

				updates = xDoc.GetElementsByTagName("updates")(0).InnerText
				suppress = xDoc.GetElementsByTagName("suppress")(0).InnerText

				default_commands = xDoc.GetElementsByTagName("default_commands")(0).InnerText


				If list = "true" Then
					chkStartList.Checked = True
				Else
					chkStartList.Checked = False
				End If

				If updates = "true" Then
					chkCheckUpdates.Checked = True
				Else
					chkCheckUpdates.Checked = False
				End If

				If altload = "true" Then
					chkWebPage.Checked = True
				Else
					chkWebPage.Checked = False
				End If

				If suppress = "true" Then
					chkSuppress.Checked = True
				Else
					chkSuppress.Checked = False
				End If

				If default_commands = "true" Then
					chkCommands.Checked = True
				Else
					chkCommands.Checked = False
				End If


			Catch ex As Exception
				chkStartList.Checked = True
				chkCheckUpdates.Checked = True
				chkWebPage.Checked = False
				chkSuppress.Checked = True
				chkCommands.Checked = False
			End Try
		End Sub

		Private Sub Options_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
			LoadSettings()
		End Sub

		Private Sub chkWebPage_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkWebPage.CheckedChanged
			If chkWebPage.Checked = True Then
				txtWeb.Enabled = True
			Else
				txtWeb.Enabled = False
			End If
		End Sub

		Private Sub linkLabel1_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs) Handles linkLabel1.LinkClicked
			System.Diagnostics.Process.Start("http://apps.ohlulz.com/rtmpgui/users/")
		End Sub

		Private Sub chkStartList_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkStartList.CheckedChanged
			If chkStartList.Checked = True Then
				txtLocList.Enabled = False
				button4.Enabled = False
			Else
				txtLocList.Enabled = True
				button4.Enabled = True
			End If

		End Sub

		Private Sub Options_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
			Me.Hide()
			e.Cancel = True
		End Sub



	End Class
End Namespace